package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.Client.ItemClient;
import com.hmall.common.Client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.OderDTO;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.UserHolder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderLogisticsService orderLogisticsService;
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;

    /**
     * 创建订单
     *
     * @param oderDTO
     * @return
     */
    @Override
    public Long createOrder(OderDTO oderDTO) {
        //-调用啊api查出来的商品信息
        Item item = itemClient.queryItemById(oderDTO.getItemId());
        Long price = item.getPrice();
        Order order = new Order();
        Integer num = oderDTO.getNum();
        //计算订单总价
        long totalFee = price * num;
        order.setPaymentType(oderDTO.getPaymentType());
        order.setTotalFee(totalFee);
        order.setUserId(UserHolder.getUserId());
        order.setStatus(1);
        //将订单信息保存到数据库
        this.save(order);
        //将order信息封装到orderdetail
        OrderDetail orderDetail = new OrderDetail();
        //这里的库表明和实体类的字段名不一致可以改实体类的字段名也可以加注解，我选择改名字~
        orderDetail.setName(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setItemId(item.getId());
        orderDetail.setNum(oderDTO.getNum());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetail.setOrderId(order.getId());
        //将订单详情数据写入数据库
        orderDetailService.save(orderDetail);
        Address address = userClient.findAddressById(oderDTO.getAddressId());
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address, orderLogistics);
        orderLogistics.setOrderId(order.getId());
        orderLogisticsService.save(orderLogistics);
        itemClient.decreaseStock(oderDTO.getItemId(), oderDTO.getNum());
        //发送消息，确认订单
        Message message = MessageBuilder.withBody(
                order.getId().toString().getBytes(StandardCharsets.UTF_8)
        ).setHeader("x-delay", 10000).build();
        rabbitTemplate.convertAndSend("delay.exchange", "delay", message);

        return order.getId();


    }

    /**
     * 订单支付
     *
     * @param orderId
     */
    @Override
    public void orderPay(Long orderId) {
        OrderDetail orderDetail = orderDetailService.getById(orderId);
        Long price = orderDetail.getPrice();
        Integer num = orderDetail.getNum();
        Order order = this.getById(orderId);
        order.setTotalFee(price * num);
        order.setStatus(2);
        this.updateById(order);

    }

    /**
     * 根据订单id修改订单状态
     *
     * @param orderId
     */
    @Override
    public void updateStatus(Long orderId) {
        Order order = this.getById(orderId);
        Integer status = order.getStatus();
        if (status == 1) {
            order.setStatus(5);
            this.updateById(order);
        }
    }


}
